<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workflow:model:add']"
        >新增模型</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workflow:model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workflow:model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模型编号" align="center" prop="id"/>
      <el-table-column label="模型名称" align="center" prop="name"/>
      <el-table-column label="模型KEY" align="center" prop="key"/>
      <el-table-column label="描述" align="center" prop="description"/>
      <el-table-column label="创建人" align="center" prop="createdBy"/>
      <el-table-column label="版本号" align="center" prop="version"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-guide"
            @click="handleDeploy(scope.row)"
            v-hasPermi="['workflow:model:deploy']"
          >部署</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workflow:model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workflow:model:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body>
      <template>
          <div >
              <iframe :src="external_url" id="map" scrolling="no" frameborder="0" width="100%" :height="dialogHeight"></iframe>
          </div>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel, exportModel, deployModel } from "@/api/workflow/model";

export default {
  name: "Model",
  components: {
  },
  data() {
    return {
      dialogHeight:"900px",
      external_url:'http://localhost:9080/flowable/#/',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型管理表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    
  },
  mounted(){

  },
  methods: {
    /**
      * iframe-宽高自适应显示   
      */
    changeMapIframe() {
      debugger
      const deviceWidth = document.body.clientWidth;
      const deviceHeight = document.body.clientHeight;
      this.dialogHeight = (Number(deviceHeight)) + 'px';
        // const map = document.getElementById('map');
        // map.style.width = (Number(deviceWidth)-240) + 'px'; //数字是页面布局宽度差值
        // map.style.height = (Number(deviceHeight)+764) + 'px'; //数字是页面布局高度差
    },
    /** 查询模型管理列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        console.log(response.rows);
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        key: null,
        description: null,
        createdBy: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.changeMapIframe();
      // this.reset();
      // this.open = true;
      // this.title = "添加模型管理";
      // this.external_url = this.external_url + 'processes';
      
      let flowableUrl = "http://"+window.location.host+":9080/flowable/#/";
      window.open(flowableUrl + 'processes','_blank');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      let flowableUrl = "http://"+window.location.host+":9080/flowable/#/";
      window.open(flowableUrl + 'editor/' + id,'_blank'); // 在新窗口打开外链接
      // getModel(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改模型管理";
      // });
    },
    /** 部署按钮操作 */
    handleDeploy(row){
      const id = row.id || this.ids
        this.$confirm('是否将模型"' + row.name + '"部署为流程?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deployModel(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("部署成功");
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除模型管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delModel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有模型管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportModel(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
